package hejie.admin.dao.impl;

import java.sql.SQLException;
import java.util.List;

import javax.annotation.Resource;

import hejie.admin.dao.AdminDao;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Component; 

import publics.model.Admin;

/**
 * @author  hejie
 * @version  1.0
 *  
 * 管理员操作Dao层接口实现
 */
//注入
@Component("adminDao")
public class AdminDaoImpl implements AdminDao{
	HibernateTemplate hibernateTemplate;// 注入hibernate的spring代理模版

	public HibernateTemplate getHibernateTemplate() {
		return hibernateTemplate;
	}

	// 注入 hibernate模版
	@Resource(name = "hibernateTemplate")	
	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
		System.out.println("hibernateTemplate injected in  AdminDaoImpl.");
		// 测试用，实际运行中可去掉这句
	}
	 

	public Integer save(Admin admin){
		return (Integer)this.getHibernateTemplate().save(admin);
	}
	
	public boolean update(Admin admin){
		try{
			this.getHibernateTemplate().update(admin);
		}catch (DataAccessException e) {
			return false;
		}
		return true;
	}
	
    public boolean deleteById(Integer id){
		System.out.println("AdminDaoImpl里 id="+id);
    	try{// 使用try 来确定 删除是否成功
			this.hibernateTemplate.delete(this.getHibernateTemplate().get(Admin.class, id));
    		
		}catch (DataAccessException e) {
			return false;
		}
		return true;
	}
    
	public boolean delete(Admin admin){
		try {
			 this.getHibernateTemplate().delete(admin);
		} catch (DataAccessException e) {
			return false;
		}
	    return true;
	    	
	}
	
	public Admin findById(Integer id){		
		return this.getHibernateTemplate().get(Admin.class, id);
	}
	
	@SuppressWarnings("unchecked")
	public List<Admin> findByUser(String user) {
		return this.getHibernateTemplate().find("from Admin a where a.user = ? " , user);
	}
	
    @SuppressWarnings("unchecked")
	public Admin findByUserAndPass(String user,String pass) {
		Object[] args = {user,pass};
	    List lists = getHibernateTemplate().find("from Admin where Ad_user = ? and Ad_pass = ?" , args);           
	    if(lists.size() == 0){
	    	return null;
	    }else {
			return (Admin)lists.get(0);
		}
	}
    
   @SuppressWarnings("unchecked")
   public List<Admin> findByPage(final String hql, final int offset, final int pageSize) {
		List<Admin> list = this.getHibernateTemplate().executeFind(
				new HibernateCallback() {
					public Object doInHibernate(Session session) throws HibernateException ,SQLException {
						//执行hibernate分页查询
						List<Admin> result = session.createQuery(hql)
							.setFirstResult(offset)
							.setMaxResults(pageSize)
							.list();
						return result;
					}
				}
				);
		
		return list;
	}
   
   public int findByCount(String sql) {	
		return this.getHibernateTemplate().find(sql).size();
		 
   }

	@SuppressWarnings("unchecked")
	public List<Admin> find(String sql) {
		return this.getHibernateTemplate().find(sql);
	}	
}
